package com.wht.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wht.domain.DtsAdmin;
import com.wht.mapper.DtsAdminMapper;
import com.wht.service.DtsAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * @author wanghaotian
 * time:2022/12/01,0001 22:11
 * description:
 */

@Service
public class DtsAdminServiceImpl implements DtsAdminService {

    @Autowired
    private DtsAdminMapper adminMapper;

    @Override
    public List<DtsAdmin> findAdmin(String username) {
        QueryWrapper<DtsAdmin> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username);
        wrapper.eq("deleted", false);
        List<DtsAdmin> list = adminMapper.selectList(wrapper);
        return list;
    }

    @Override
    public IPage<DtsAdmin> querySelective(String username, Integer page, Integer limit, String sort, String order) {
        IPage<DtsAdmin> iPage = new Page<>(page,limit);
        QueryWrapper<DtsAdmin> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(username)) {
            wrapper.like("username", username);
        }
        wrapper.eq("deleted", false);
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            if (order.equalsIgnoreCase("asc")) {
                wrapper.orderByAsc(sort);
            }else {
                wrapper.orderByDesc(sort);
            }
        }
        iPage = adminMapper.selectPage(iPage, wrapper);
        return iPage;
    }

    @Override
    public void add(DtsAdmin admin) {
        admin.setAddTime(new Date());
        admin.setUpdateTime(new Date());
        adminMapper.insert(admin);
    }

    @Override
    public int updateById(DtsAdmin admin) {
        admin.setUpdateTime(new Date());
        return adminMapper.updateById(admin);
    }

    @Override
    public void deleteById(Integer id) {
        adminMapper.deleteById(id);
    }
}
